home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Graphics Samples / ShapePart Browser ƒ / Window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-15  |  7.7 KB  |  344 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    Window.c 
  3.  */
  4.  
  5. #include "graphics routines.h"
  6. #include "graphics libraries.h"    /* For commonColors */
  7.  
  8. #include "ResourceIDs.h"
  9. #include "ShapeAction.h"
  10. #include "ShapeSetup.h"
  11.  
  12. #include "Window.h"
  13.  
  14.  
  15. /*----------------------*/
  16. /*    Global Declarations    */
  17. /*----------------------*/
  18. GlobalStructure    globals;
  19.  
  20.  
  21. /*------------------------------*/
  22. /*    External Declarations     */
  23. /*------------------------------*/
  24.  
  25.  
  26. /*------------------------------*/
  27. /*       Local ProtoTypes       */
  28. /*------------------------------*/
  29. void GetShapePartControls (WindowPtr pWindow, short firstItem, short lastItem, ControlHandle *ph1stControl);
  30. void TrackCheckBox (ControlHandle hControl, Point where, Boolean *pIsChecked);
  31. void SetShapePartsFromFlags (GlobalStructure *pG, ControlHandle hControl, long shapeCount);
  32. void TrackShapePartCheckBox (ControlHandle hControlHit, short whichPart, Point where);
  33.  
  34.  
  35.     void
  36. GetShapePartControls (WindowPtr pWindow, short firstItem, short lastItem, ControlHandle *ph1stControl)
  37. {
  38.     register
  39.     short            item;
  40.     register
  41.     ControlHandle    *pControl;
  42.  
  43.     pControl = ph1stControl;
  44.     for (item = firstItem; item <= lastItem; item++)
  45.     {
  46.         GetDItem (pWindow, item, nil, (Handle *) pControl, nil);
  47.         if (*pControl == nil)
  48.         {
  49.             DebugStr ("\pGetShapePartControls: No checkbox found");
  50.             break;
  51.         }
  52.         pControl++;
  53.     }
  54. }
  55.  
  56.  
  57.     Boolean
  58. InitializeHitTesting (WindowPtr pWindow)
  59. {
  60.     if (! CreateShapesFrame (pWindow, kRowCount, kColumnCount, &globals.boxes))
  61.         return (false);
  62.  
  63.     if (! CreateHitTestShapes (pWindow, &globals.pShapes, &globals.boxes, kShapeCount))
  64.         return (false);
  65.  
  66.     GetShapePartControls (pWindow, checkBoxFirstShapePart, checkBoxLastShapePart, &globals.hNothing);
  67.  
  68.     /* Force SetShapePartsFromFlags to initialize the shape's gxShape parts */
  69.     globals.partsHit     = gxAnyPart;
  70.     globals.tolerance    = ff(5);
  71.  
  72.     globals.showControlPoints = false;
  73.     globals.showLocalBounds   = false;
  74.  
  75.     SetShapePartsFromFlags (&globals, globals.hNothing, kShapeCount);
  76.  
  77.     return (true);
  78. }
  79.  
  80.  
  81.     void
  82. CleanupHitTesting (void)
  83. {
  84.     DisposeHitTestShapes (&globals.pShapes, &globals.boxes);
  85. }
  86.  
  87.  
  88.     void
  89. IdleHitTestWindow (WindowPtr pWindow, EventRecord *pEvent)
  90. {
  91.     Point    where;
  92.  
  93.     where = pEvent->where;
  94.     GlobalToLocal (&where);
  95.  
  96.     if (PtInRect (where, &pWindow->portRect))
  97.         UpdateShapePartInfo (&where, &globals);
  98. }
  99.  
  100.  
  101.     void
  102. TrackCheckBox (ControlHandle hControl, Point where, Boolean *pIsChecked)
  103. {
  104.     if (TrackControl (hControl, where, nil))
  105.     {
  106.         *pIsChecked = ! *pIsChecked;
  107.         SetCtlValue (hControl, *pIsChecked);
  108.     }
  109. }
  110.  
  111.  
  112. /*
  113.  *    pG->hNothing
  114.  *    pG->hBounds
  115.  *    pG->hAnything
  116.  *
  117.  *    pG->(all)Checked
  118.  *
  119.  *    pG->partsHit
  120.  */
  121. /*
  122.     Sets all the gxShape's gxShapeParts from the state of the check-boxes
  123. */
  124.     void
  125. SetShapePartsFromFlags (GlobalStructure *pG, ControlHandle hControl, long shapeCount)
  126. {
  127.     register
  128.     gxShape            *pShape;
  129.     gxShapePart        newParts;
  130.     Boolean            *pFlag;
  131.     ControlHandle    *pControl;
  132.  
  133.     if (hControl == pG->hNothing)
  134.     {
  135.         if (pG->testNothing)
  136.         {
  137.             /* Nothing checkbox just checked on; turn off all other checkboxes */
  138.             newParts = gxNoPart;
  139.  
  140.             pFlag     = &pG->testBounds;
  141.             pControl = &pG->hBounds;
  142.  
  143.             while (pFlag <= &pG->testAnything)
  144.                 SetCtlValue (*pControl++, *pFlag++ = false);
  145.         }
  146.         else
  147.         {
  148.             /* Nothing was just checked off; turn on all other checkboxes */
  149.             pFlag     = &pG->testBounds;
  150.             pControl = &pG->hBounds;
  151.  
  152.             newParts = gxAnyPart;
  153.             while (pFlag <= &pG->testAnything)
  154.                 SetCtlValue (*pControl++, *pFlag++ = true);
  155.  
  156.             SetCtlValue (pG->hNothing, pG->testNothing = false);
  157.         }
  158.     }
  159.     else if (hControl == pG->hAnything) 
  160.     {
  161.         Boolean        newState;
  162.  
  163.         /* Anything checkbox hit */
  164.         if (pG->testAnything)
  165.         {
  166.             /* Turn on all checkboxes if Anything was already checked */
  167.             newState = true;
  168.             newParts = pG->partsHit | gxAnyPart;
  169.         }
  170.         else
  171.         {
  172.             /* Turn off all checkboxes */
  173.             newState = false;
  174.             newParts = pG->partsHit & ~gxAnyPart;
  175.         }
  176.  
  177.         pFlag     = &pG->testBounds;
  178.         pControl = &pG->hBounds;
  179.  
  180.         while (pFlag <= &pG->testPattern)
  181.             SetCtlValue (*pControl++, *pFlag++ = newState);
  182.  
  183.         /* Turn off the Nothing checkbox */
  184.         SetCtlValue (pG->hNothing, pG->testNothing = false);
  185.     }
  186.     else
  187.     {
  188.         /* An individual checkbox was hit */
  189.         newParts = gxNoPart;
  190.  
  191.         if (pG->testNothing)
  192.             SetCtlValue (pG->hNothing, pG->testNothing = false);
  193.         if (pG->testBounds)            newParts |= gxBoundsPart;
  194.         if (pG->testGeometry)        newParts |= gxGeometryPart;
  195.         if (pG->testPen)            newParts |= gxPenPart;
  196.         if (pG->testCornerPoint)    newParts |= gxCornerPointPart;
  197.         if (pG->testControlPoint)    newParts |= gxControlPointPart;
  198.         if (pG->testEdge)            newParts |= gxEdgePart;
  199.         if (pG->testJoin)            newParts |= gxJoinPart;
  200.         if (pG->testStartCap)        newParts |= gxStartCapPart;
  201.         if (pG->testEndCap)            newParts |= gxEndCapPart;
  202.         if (pG->testDash)            newParts |= gxDashPart;
  203.         if (pG->testPattern)        newParts |= gxPatternPart;
  204.  
  205.         if (pG->testAnything    &&
  206.             (newParts & gxAnyPart) != gxAnyPart)
  207.             SetCtlValue (pG->hAnything, pG->testAnything = false);
  208.     }
  209.  
  210.     if (newParts == gxNoPart)
  211.         SetCtlValue (pG->hNothing, pG->testNothing = true);
  212.     else if ((newParts & gxAnyPart) == gxAnyPart  &&
  213.              (! pG->testAnything))
  214.     {
  215.         SetCtlValue (pG->hAnything, pG->testAnything = true);
  216.     }
  217.  
  218.     /* Set the new parts tested on all shapes */
  219.     for (pShape = pG->pShapes + shapeCount - 1;
  220.          pShape >= pG->pShapes;
  221.          pShape--)
  222.     {
  223.         GXSetShapeHitTest (*pShape, newParts, pG->tolerance);
  224.     }
  225.     pG->partsHit = newParts;
  226. }
  227.  
  228.  
  229.     void
  230. TrackShapePartCheckBox (ControlHandle hControlHit, short whichPart, Point where)
  231. {
  232.     Rect    bounds;
  233.  
  234.     if (hControlHit == globals.hNothing)
  235.     {    TrackCheckBox (globals.hNothing, where, &globals.testNothing);
  236.         return;
  237.     }
  238.  
  239.     if (hControlHit == globals.hBounds)
  240.     {    TrackCheckBox (globals.hBounds, where, &globals.testBounds);
  241.         return;
  242.     }
  243.  
  244.     if (hControlHit == globals.hGeometry)
  245.     {    TrackCheckBox (globals.hGeometry, where, &globals.testGeometry);
  246.         return;
  247.     }
  248.  
  249.     if (hControlHit == globals.hPen)
  250.     {    TrackCheckBox (globals.hPen, where, &globals.testPen);
  251.         return;
  252.     }
  253.  
  254.     if (hControlHit == globals.hCornerPoint)
  255.     {    TrackCheckBox (globals.hCornerPoint, where, &globals.testCornerPoint);
  256.         return;
  257.     }
  258.  
  259.     if (hControlHit == globals.hControlPoint)
  260.     {    TrackCheckBox (globals.hControlPoint, where, &globals.testControlPoint);
  261.         return;
  262.     }
  263.  
  264.     if (hControlHit == globals.hEdge)
  265.     {    TrackCheckBox (globals.hEdge, where, &globals.testEdge);
  266.         return;
  267.     }
  268.  
  269.     if (hControlHit == globals.hJoin)
  270.     {    TrackCheckBox (globals.hJoin, where, &globals.testJoin);
  271.         return;
  272.     }
  273.  
  274.     if (hControlHit == globals.hStartCap)
  275.     {    TrackCheckBox (globals.hStartCap, where, &globals.testStartCap);
  276.         return;
  277.     }
  278.  
  279.     if (hControlHit == globals.hEndCap)
  280.     {    TrackCheckBox (globals.hEndCap, where, &globals.testEndCap);
  281.         return;
  282.     }
  283.  
  284.     if (hControlHit == globals.hDash)
  285.     {    TrackCheckBox (globals.hDash, where, &globals.testDash);
  286.         return;
  287.     }
  288.  
  289.     if (hControlHit == globals.hPattern)
  290.     {    TrackCheckBox (globals.hPattern, where, &globals.testPattern);
  291.         return;
  292.     }
  293.  
  294.     TrackCheckBox (globals.hAnything, where, &globals.testAnything);
  295. }
  296.  
  297.  
  298.     void
  299. DoHitTestContent (EventRecord *pEvent, WindowPtr pWindow)
  300. {
  301.     Point            where;
  302.     ControlHandle    hControl;
  303.     short            partCode;
  304.  
  305.     where = pEvent->where;
  306.     GlobalToLocal (&where);
  307.  
  308.     partCode = FindControl (where, pWindow, &hControl);
  309.  
  310.     if (hControl != nil)
  311.     {
  312.         TrackShapePartCheckBox (hControl, partCode, where);
  313.         SetShapePartsFromFlags (&globals, hControl, kShapeCount);
  314.     }
  315. }
  316.  
  317.  
  318.     void
  319. ToggleMetricsMenuItem (short item, WindowPtr pWIndow)
  320. {
  321.     Boolean        *pState;
  322.     Str255        menuItemStr;
  323.  
  324.     switch (item)
  325.     {
  326.         case itemShowControlPoints:
  327.             pState = &globals.showControlPoints;
  328.             break;
  329.  
  330.         case itemShowLocalBounds:
  331.             pState = &globals.showLocalBounds;
  332.             break;
  333.     }
  334.  
  335.     *pState = ! *pState;
  336.     GetIndString (menuItemStr, metricsMenuStringsID, *pState + 2 * item - itemShowControlPoints);
  337.     SetItem (GetMHandle (metricsMenuRsrcID), item, menuItemStr);
  338.  
  339.     if (*pState  &&  item == itemShowControlPoints)
  340.         ShowControlPoints (globals.pShapes, kShapeCount);
  341.     else
  342.         UpdateHitTestWindow (pWIndow);
  343. }
  344.